package com.futer.ssm.util;

import java.util.Date;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail {

    //===========================注册===================================
    public static String sengEmail(String email, Date date, String name,String type){
        try{
            Properties prop = new Properties();
            prop.setProperty("mail.host", "smtp.sohu.com");
            prop.setProperty("mail.transport.protocol", "smtp");
            prop.setProperty("mail.smtp.auth", "true");
            prop.setProperty("mail.smtp.port","465");
            prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            prop.setProperty("mail.smtp.socketFactory.fallback", "false");
            prop.setProperty("mail.smtp.socketFactory.port", "465");
            //使用JavaMail发送邮件的5个步骤
            //1、创建session
            Session session = Session.getInstance(prop);
            //开启Session的debug模式，这样就可以查看到程序发送Email的运行状态
            session.setDebug(true);
            //2、通过session得到transport对象
            Transport ts = session.getTransport();
            //3、使用邮箱的用户名和密码连上邮件服务器，发送邮件时，发件人需要提交邮箱的用户名和密码给smtp服务器，用户名和密码都通过验证之后才能够正常发送邮件给收件人。
            ts.connect("smtp.163.com", "13078260779", "5891652010");
            //4、创建邮件
            Message message = createSimpleMail(session,email,date,name,type);
            //5、发送邮件
            ts.sendMessage(message, message.getAllRecipients());
            ts.close();
            return "success";
        }catch (NullPointerException e){
            System.out.println("邮件发送错误，原因："+e.getLocalizedMessage());
            return "fale,"+e.getLocalizedMessage();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
            System.out.println("邮件发送错误，原因："+e.getLocalizedMessage());
            return "fale,"+e.getLocalizedMessage();
        } catch (MessagingException e) {
            e.printStackTrace();
            System.out.println("邮件发送错误，原因："+e.getLocalizedMessage());
            return "fale,"+e.getLocalizedMessage();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("邮件发送错误，原因："+e.getLocalizedMessage());
            return "fale,"+e.getLocalizedMessage();
        }

    }
    private static MimeMessage createSimpleMail(Session session,String email,Date date,String name,String type)
            throws Exception {
        //创建邮件对象
        MimeMessage message = new MimeMessage(session);
        //指明邮件的发件人
        message.setFrom(new InternetAddress("13078260779@163.com"));
        //指明邮件的收件人，现在发件人和收件人是一样的，那就是自己给自己发
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
        //邮件的文本内容http://localhost:8080/futer/makeActivity?name="+name+"&key="+date.getTime()+"
        if (type.equals("注册")){
            //邮件的标题
            message.setSubject("Flutter账号激活");
            message.setContent("<p>欢迎使用flutter框架，请点链接进行账号激活<a href='http://114.116.149.238:8080/futer/makeActivity?name="+name+"&key="+date.getTime()+"'>激活</a> </p>", "text/html;charset=GBK");
        }else {
            //邮件的标题
            message.setSubject("Flutter密码找回");
            message.setContent("<p>欢迎使用flutter框架，验证码是：<b>"+name+"</b>。此验证码的有限期为10分钟！请尽快使用", "text/html;charset=GBK");
        }

        //message.setContent(date.getTime()+"https://www.baidu.com", "text/html;charset=UTF-8");
        //返回创建好的邮件对象
        return message;
    }
    //===========================注册End===================================

}
